from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QAction,\
                            QMainWindow,\
                            QMenu

from RBK.View.Tab        import Tab
from RBK.View.NewDialog  import NewDialog
from RBK.View.OpenDialog import OpenDialog
from RBK.View.EditDialog import EditDialog


class MainWindow(QMainWindow):
    def __init__(self, db):
        super().__init__()

        self.setWindowTitle('RulezGen')
        self.resize(800, 600)
        self.db = db
        self.models = db.getModels()
        self.models['db'] = self.db
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction("&New", self.onFileNew, QKeySequence(QKeySequence.New))
        self.fileMenu.addAction("&Open", self.onFileOpen, QKeySequence(QKeySequence.Open))
        self.fileMenu.addSeparator()
        #Rulebook Properties is a separate action so I can enable/disable as needed.
        self.rbkproperties = QAction('&Rulebook Properties', self)
        self.rbkproperties.triggered.connect(self.onFileRulebookProperties)
        self.rbkproperties.setEnabled(False)
        self.fileMenu.addAction(self.rbkproperties)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction("E&xit", self.onFileExit, QKeySequence(QKeySequence.Quit))

        self.newDialog = None
        self.openDialog = None

        self.tab = Tab()
        self.show()

    @pyqtSlot()
    def onFileNew(self):
        if self.newDialog is None:
            self.newDialog = NewDialog(self)
            self.newDialog.newed.connect(self.onFileNewAccepted)
        self.newDialog.exec()

    @pyqtSlot(str, str, str, str, str)
    def onFileNewAccepted(self, g, v, r, c, cr):
        self.newDialog = None
        self.db.closeRulebook()
        self.db.newRulebook(g, v, r, c, cr)
        self.models = self.db.getModels()
        self.models['db'] = self.db

        self.tab.setModel(self.models)
        self.tab.setCore(self.db.isCore(self.db.db))
        self.rbkproperties.setEnabled(True)
        self.setCentralWidget(self.tab)

    @pyqtSlot()
    def onFileOpen(self):
        if self.openDialog is None:
            self.openDialog = OpenDialog(self, self.models['rulebooks'])
            self.openDialog.opened.connect(self.onFileOpenAccepted)
        self.openDialog.exec()

    @pyqtSlot(str)
    def onFileOpenAccepted(self, rbkfile):
        self.openDialog = None
        self.db.closeRulebook()
        self.db.openRulebook(rbkfile)
        self.models = self.db.getModels()
        self.models['db'] = self.db
        self.tab.setModel(self.models)
        self.tab.setCore(self.db.isCore(self.db.db))
        self.rbkproperties.setEnabled(True)
        self.setCentralWidget(self.tab)


    @pyqtSlot()
    def onFileRulebookProperties(self):
        edit = EditDialog(self, self.models['Rulebook'], self.models['rulebooks'])
        edit.exec()

    @pyqtSlot()
    def onFileExit(self):
        self.db.closeRulebook()
        self.close()
